

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Blue~u~u~u">
  <meta name="author" content="Blue~u~u~u">
  <meta name="keywords" content="">
  <meta name="description" content="springmvc-day01第一章 SpringMVC入门第一节 SpringMVC概述1. SpringMVC 优势SpringMVC 是 Spring 为表述层开发提供的一整套完备的解决方案。在表述层框架历经 Strust、WebWork、Strust2 等诸多产品的历代更迭之后，目前业界普遍选择了 SpringMVC 作为 Java EE 项目表述层开发的首选方案。之所以能做到这一点，是因">
<meta property="og:type" content="article">
<meta property="og:title" content="springMVC-01">
<meta property="og:url" content="http://www.slx.blue/2021/12/10/springMVC-01/index.html">
<meta property="og:site_name" content="Blue~u~u~u~u">
<meta property="og:description" content="springmvc-day01第一章 SpringMVC入门第一节 SpringMVC概述1. SpringMVC 优势SpringMVC 是 Spring 为表述层开发提供的一整套完备的解决方案。在表述层框架历经 Strust、WebWork、Strust2 等诸多产品的历代更迭之后，目前业界普遍选择了 SpringMVC 作为 Java EE 项目表述层开发的首选方案。之所以能做到这一点，是因">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_001.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_002.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_003.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_004.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_006.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_007.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_009.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_010.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_011.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_008.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_014.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_015.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_016.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_017.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_012.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_013.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_018.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_019.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_020.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_021.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_022.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_023.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_021.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_025.png">
<meta property="article:published_time" content="2021-12-10T12:27:57.000Z">
<meta property="article:modified_time" content="2021-12-10T12:32:46.073Z">
<meta property="article:author" content="Blue~u~u~u">
<meta property="article:tag" content="springMVC">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://www.slx.blue/2021/12/10/springMVC-01/tu_001.png">
  
  <title>springMVC-01 - Blue~u~u~u~u</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.slx.blue","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":180,"cursorChar":"_","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"/>
<script src="/live2d-widget/autoload.js"></script>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Blue~u~u</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/images/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="springMVC-01">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-12-10 20:27" pubdate>
        2021年12月10日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      45k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      142 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">springMVC-01</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：6 个月前
                
              </p>
            
            <div class="markdown-body">
              <h1 id="springmvc-day01"><a href="#springmvc-day01" class="headerlink" title="springmvc-day01"></a>springmvc-day01</h1><h2 id="第一章-SpringMVC入门"><a href="#第一章-SpringMVC入门" class="headerlink" title="第一章 SpringMVC入门"></a>第一章 SpringMVC入门</h2><h3 id="第一节-SpringMVC概述"><a href="#第一节-SpringMVC概述" class="headerlink" title="第一节 SpringMVC概述"></a>第一节 SpringMVC概述</h3><h4 id="1-SpringMVC-优势"><a href="#1-SpringMVC-优势" class="headerlink" title="1. SpringMVC 优势"></a>1. SpringMVC 优势</h4><p>SpringMVC 是 Spring 为表述层开发提供的一整套完备的解决方案。在表述层框架历经 Strust、WebWork、Strust2 等诸多产品的历代更迭之后，目前业界普遍选择了 SpringMVC 作为 Java EE 项目表述层开发的<strong>首选方案</strong>。之所以能做到这一点，是因为 SpringMVC 具备如下显著优势：</p>
<ul>
<li><strong>Spring 家族原生产品</strong>，与 IOC 容器等基础设施无缝对接</li>
<li>表述层各细分领域需要解决的问题<strong>全方位覆盖</strong>，提供<strong>全面解决方案</strong></li>
<li><strong>代码清新简洁</strong>，大幅度提升开发效率</li>
<li>内部组件化程度高，可插拔式组件<strong>即插即用</strong>，想要什么功能配置相应组件即可</li>
<li><strong>性能卓著</strong>，尤其适合现代大型、超大型互联网项目要求</li>
</ul>
<h4 id="2-表述层框架要解决的基本问题"><a href="#2-表述层框架要解决的基本问题" class="headerlink" title="2. 表述层框架要解决的基本问题"></a>2. 表述层框架要解决的基本问题</h4><ul>
<li>请求映射:让一个方法处理一个请求</li>
<li>数据输入:获取请求参数</li>
<li>类型转换:内置或者自定义类型转换器，对请求参数的类型进行转换</li>
<li>数据校验:参数的非空校验</li>
<li>视图界面:Thymeleaf</li>
<li>请求分发:请求转发</li>
<li>与域对象交互</li>
<li>会话控制:Session</li>
<li>过滤拦截:Filter</li>
<li>异步交互</li>
<li>文件上传</li>
<li>文件下载</li>
</ul>
<h3 id="第二节-SpringMVC入门案例-重点"><a href="#第二节-SpringMVC入门案例-重点" class="headerlink" title="第二节 SpringMVC入门案例(重点)"></a>第二节 SpringMVC入门案例(重点)</h3><h4 id="1-功能需求"><a href="#1-功能需求" class="headerlink" title="1. 功能需求"></a>1. 功能需求</h4><h5 id="1-1-访问首页"><a href="#1-1-访问首页" class="headerlink" title="1.1 访问首页"></a>1.1 访问首页</h5><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_001.png" srcset="/img/loading.gif" lazyload style="zoom: 80%;">

<h5 id="1-2-在首页点超链接"><a href="#1-2-在首页点超链接" class="headerlink" title="1.2 在首页点超链接"></a>1.2 在首页点超链接</h5><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_002.png" srcset="/img/loading.gif" lazyload style="zoom:80%;">

<h4 id="2-搭建环境"><a href="#2-搭建环境" class="headerlink" title="2. 搭建环境"></a>2. 搭建环境</h4><h5 id="2-1-导入依赖"><a href="#2-1-导入依赖" class="headerlink" title="2.1 导入依赖"></a>2.1 导入依赖</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- SpringMVC --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-webmvc<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.3.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- 日志 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>ch.qos.logback<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>logback-classic<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.3<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- ServletAPI --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>javax.servlet<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>javax.servlet-api<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.1.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>provided<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- Spring5和Thymeleaf整合包 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.thymeleaf<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>thymeleaf-spring5<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.0.12.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>由于 Maven 的传递性，我们不必将所有需要的包全部配置依赖，而是配置最顶端的依赖，其他靠传递性导入。 </p>
<p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_003.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="2-2-日志配置文件"><a href="#2-2-日志配置文件" class="headerlink" title="2.2 日志配置文件"></a>2.2 日志配置文件</h5><p>文件名：logback.xml </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">configuration</span> <span class="hljs-attr">debug</span>=<span class="hljs-string">&quot;true&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 指定日志输出的位置 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">appender</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;STDOUT&quot;</span></span><br><span class="hljs-tag">              <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;ch.qos.logback.core.ConsoleAppender&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">encoder</span>&gt;</span><br>            <span class="hljs-comment">&lt;!-- 日志输出的格式 --&gt;</span><br>            <span class="hljs-comment">&lt;!-- 按照顺序分别是：时间、日志级别、线程名称、打印日志的类、日志主体内容、换行 --&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">pattern</span>&gt;</span>[%d&#123;HH:mm:ss.SSS&#125;] [%-5level] [%thread] [%logger] [%msg]%n<span class="hljs-tag">&lt;/<span class="hljs-name">pattern</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">encoder</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">appender</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- 设置全局日志级别。日志级别按顺序分别是：DEBUG、INFO、WARN、ERROR --&gt;</span><br>    <span class="hljs-comment">&lt;!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">root</span> <span class="hljs-attr">level</span>=<span class="hljs-string">&quot;DEBUG&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- 指定打印日志的appender，这里通过“STDOUT”引用了前面配置的appender --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">appender-ref</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;STDOUT&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">root</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- 根据特殊需求指定局部日志级别 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">logger</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;org.springframework.web.servlet.DispatcherServlet&quot;</span> <span class="hljs-attr">level</span>=<span class="hljs-string">&quot;DEBUG&quot;</span> /&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-3-web-xml配置"><a href="#2-3-web-xml配置" class="headerlink" title="2.3 web.xml配置"></a>2.3 web.xml配置</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 配置SpringMVC中负责处理请求的核心Servlet，也被称为SpringMVC的前端控制器 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>DispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- DispatcherServlet的全类名 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- 通过初始化参数指定SpringMVC配置文件位置 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>    <br>        <span class="hljs-comment">&lt;!-- 如果不记得contextConfigLocation配置项的名称，可以到DispatcherServlet的父类FrameworkServlet中查找 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>contextConfigLocation<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>    <br>        <span class="hljs-comment">&lt;!-- 使用classpath:说明这个路径从类路径的根目录开始才查找 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>classpath:spring-mvc.xml<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- 作为框架的核心组件，在启动过程中有大量的初始化操作要做，这些操作放在第一次请求时才执行非常不恰当 --&gt;</span><br>    <span class="hljs-comment">&lt;!-- 我们应该将DispatcherServlet设置为随Web应用一起启动 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span><br>    <br><span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span><br>    <br><span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>DispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- 对DispatcherServlet来说，url-pattern有两种方式配置 --&gt;</span><br>    <span class="hljs-comment">&lt;!-- 方式一：配置“/”，表示匹配整个Web应用范围内所有请求。 --&gt;</span><br>    <span class="hljs-comment">&lt;!-- 方式二：配置“*.扩展名”，表示匹配整个Web应用范围内部分请求 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-4-Spring配置文件"><a href="#2-4-Spring配置文件" class="headerlink" title="2.4 Spring配置文件"></a>2.4 Spring配置文件</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 自动扫描包 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.atguigu.mvc.handler&quot;</span>/&gt;</span><br>    <br><span class="hljs-comment">&lt;!-- Thymeleaf视图解析器 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;viewResolver&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.thymeleaf.spring5.view.ThymeleafViewResolver&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;order&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;1&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;characterEncoding&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;templateEngine&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.thymeleaf.spring5.SpringTemplateEngine&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;templateResolver&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver&quot;</span>&gt;</span><br>    <br>                    <span class="hljs-comment">&lt;!-- 视图前缀 --&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;prefix&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;/WEB-INF/templates/&quot;</span>/&gt;</span><br>    <br>                    <span class="hljs-comment">&lt;!-- 视图后缀 --&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;suffix&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;.html&quot;</span>/&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;templateMode&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;HTML5&quot;</span>/&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;characterEncoding&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;UTF-8&quot;</span> /&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="3-代码实现"><a href="#3-代码实现" class="headerlink" title="3. 代码实现"></a>3. 代码实现</h4><h5 id="3-1-创建请求处理器类"><a href="#3-1-创建请求处理器类" class="headerlink" title="3.1 创建请求处理器类"></a>3.1 创建请求处理器类</h5><h6 id="3-1-1-命名规范"><a href="#3-1-1-命名规范" class="headerlink" title="3.1.1 命名规范"></a>3.1.1 命名规范</h6><p>SpringMVC 对处理请求的类并没有特殊要求，只要是 JavaBean 即可。我们自己习惯上有两种命名方式：</p>
<ul>
<li>XxxHandler：意思是 Xxx 处理器的意思</li>
<li>XxxController：意思是 Xxx 控制器的意思</li>
</ul>
<p>这只是一个命名的习惯，不是语法要求。所以往往把处理请求的类叫做『Handler类』，处理请求的方法叫做『Handler方法』。</p>
<h6 id="3-1-2-创建处理器类"><a href="#3-1-2-创建处理器类" class="headerlink" title="3.1.2 创建处理器类"></a>3.1.2 创建处理器类</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Demo01HelloHandler</span> </span>&#123;<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-2-实现访问首页"><a href="#3-2-实现访问首页" class="headerlink" title="3.2 实现访问首页"></a>3.2 实现访问首页</h5><h6 id="3-2-1-创建-handler-方法"><a href="#3-2-1-创建-handler-方法" class="headerlink" title="3.2.1 创建 handler 方法"></a>3.2.1 创建 handler 方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Demo01Handler</span> </span>&#123;<br>    <span class="hljs-meta">@RequestMapping(&quot;/&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">showPortal</span><span class="hljs-params">()</span></span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;showPortal()方法被调用了...&quot;</span>);<br>        <span class="hljs-comment">//handler方法的返回值就是对应的Thymeleaf模板的逻辑视图</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;index&quot;</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="3-2-2-在首页编写超链接"><a href="#3-2-2-在首页编写超链接" class="headerlink" title="3.2.2 在首页编写超链接"></a>3.2.2 在首页编写超链接</h6><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_004.png" srcset="/img/loading.gif" lazyload></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span> <span class="hljs-attr">xmlns:th</span>=<span class="hljs-string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>首页<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>欢迎来到SpringMVC首页<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>        <span class="hljs-comment">&lt;!--&quot;@&#123;/&#125;&quot;表示&quot;/项目名/&quot;--&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/toTarget&#125;&quot;</span>&gt;</span>访问目标页面<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="3-3-实现点击超链接"><a href="#3-3-实现点击超链接" class="headerlink" title="3.3 实现点击超链接"></a>3.3 实现点击超链接</h5><h6 id="3-3-1-加入日志记录仪"><a href="#3-3-1-加入日志记录仪" class="headerlink" title="3.3.1 加入日志记录仪"></a>3.3.1 加入日志记录仪</h6><p>在 handler 类中声明一个成员变量：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> Logger logger = LoggerFactory.getLogger(<span class="hljs-keyword">this</span>.getClass());<br></code></pre></td></tr></table></figure>

<ul>
<li>Logger：org.slf4j.Logger</li>
<li>LoggerFactory：org.slf4j.LoggerFactory</li>
</ul>
<h6 id="3-3-2-声明-handler-方法"><a href="#3-3-2-声明-handler-方法" class="headerlink" title="3.3.2 声明 handler 方法"></a>3.3.2 声明 handler 方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/toTarget&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toTarget</span><span class="hljs-params">()</span></span>&#123;<br>    logger.debug(<span class="hljs-string">&quot;toTarget方法被调用了...&quot;</span>);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="3-3-3-创建目标页面"><a href="#3-3-3-创建目标页面" class="headerlink" title="3.3.3 创建目标页面"></a>3.3.3 创建目标页面</h6><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span> <span class="hljs-attr">xmlns:th</span>=<span class="hljs-string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>目标页面<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>欢迎来到目标页面<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="3-4-整体流程解析"><a href="#3-4-整体流程解析" class="headerlink" title="3.4 整体流程解析"></a>3.4 整体流程解析</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_006.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="第三节-常见注解"><a href="#第三节-常见注解" class="headerlink" title="第三节 常见注解"></a>第三节 常见注解</h3><h4 id="1-RequestMapping注解-重点"><a href="#1-RequestMapping注解-重点" class="headerlink" title="1. RequestMapping注解(重点)"></a>1. RequestMapping注解(重点)</h4><h5 id="1-1-作用"><a href="#1-1-作用" class="headerlink" title="1.1 作用"></a>1.1 作用</h5><p>从注解名称上我们可以看到，@RequestMapping注解的作用就是将请求的 URL 地址和处理请求的方式关联起来，建立映射关系。</p>
<p>SpringMVC 接收到指定的请求，就会来找到在映射关系中对应的方法来处理这个请求。</p>
<h5 id="1-2-使用位置"><a href="#1-2-使用位置" class="headerlink" title="1.2 使用位置"></a>1.2 使用位置</h5><h6 id="1-2-1-准备客户端访问路径"><a href="#1-2-1-准备客户端访问路径" class="headerlink" title="1.2.1 准备客户端访问路径"></a>1.2.1 准备客户端访问路径</h6><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>测试@RequestMapping注解标记在类上<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/user/login&#125;&quot;</span>&gt;</span>用户登录<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/user/register&#125;&quot;</span>&gt;</span>用户注册<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/user/logout&#125;&quot;</span>&gt;</span>用户退出<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-2-2-仅标记在方法上的RequestMapping注解"><a href="#1-2-2-仅标记在方法上的RequestMapping注解" class="headerlink" title="1.2.2 仅标记在方法上的RequestMapping注解"></a>1.2.2 仅标记在方法上的RequestMapping注解</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/user/login&quot;)</span><br><span class="hljs-meta">@RequestMapping(&quot;/user/register&quot;)</span><br><span class="hljs-meta">@RequestMapping(&quot;/user/logout&quot;)</span><br></code></pre></td></tr></table></figure>

<h6 id="1-2-3-分别标记在类和方法上的-RequestMapping注解"><a href="#1-2-3-分别标记在类和方法上的-RequestMapping注解" class="headerlink" title="1.2.3 分别标记在类和方法上的@RequestMapping注解"></a>1.2.3 分别标记在类和方法上的@RequestMapping注解</h6><p>在类级别：抽取各个方法上@RequestMapping注解地址中前面重复的部分 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/user&quot;)</span><br></code></pre></td></tr></table></figure>

<p>在方法级别：省略被类级别抽取的部分 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/login&quot;)</span><br><span class="hljs-meta">@RequestMapping(&quot;/register&quot;)</span><br><span class="hljs-meta">@RequestMapping(&quot;/logout&quot;)</span><br></code></pre></td></tr></table></figure>

<h5 id="1-3-指定请求方式"><a href="#1-3-指定请求方式" class="headerlink" title="1.3 指定请求方式"></a>1.3 指定请求方式</h5><h6 id="1-3-1-通过RequestMapping的method属性指定"><a href="#1-3-1-通过RequestMapping的method属性指定" class="headerlink" title="1.3.1 通过RequestMapping的method属性指定"></a>1.3.1 通过RequestMapping的method属性指定</h6><p>HTTP 协议定义了八种请求方式，在 SpringMVC 中封装到了下面这个枚举类： </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">enum</span> <span class="hljs-title">RequestMethod</span> </span>&#123;<br>    GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE<br>&#125;<br></code></pre></td></tr></table></figure>

<p>设置RequestMapping注解的method属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">//value属性表示匹配请求路径，method属性表示匹配请求方式</span><br><span class="hljs-meta">@RequestMapping(value = &quot;/emp&quot;, method = RequestMethod.GET)</span><br></code></pre></td></tr></table></figure>

<h6 id="1-3-2-通过RequestMapping的进阶注解指定"><a href="#1-3-2-通过RequestMapping的进阶注解指定" class="headerlink" title="1.3.2 通过RequestMapping的进阶注解指定"></a>1.3.2 通过RequestMapping的进阶注解指定</h6><table>
<thead>
<tr>
<th>原版</th>
<th>进阶版</th>
</tr>
</thead>
<tbody><tr>
<td>@RequestMapping(value = “/emp”,  method = RequestMethod.GET)</td>
<td>@GetMapping(“/emp”)</td>
</tr>
<tr>
<td>@RequestMapping(value = “/emp”,  method = RequestMethod.POST)</td>
<td>@PostMapping(“/emp”)</td>
</tr>
<tr>
<td>@RequestMapping(value = “/emp”,  method = RequestMethod.PUT)</td>
<td>@PutMapping(“/emp”)</td>
</tr>
<tr>
<td>@RequestMapping(value = “/emp”,  method = RequestMethod.DELETE)</td>
<td>@DeleteMapping(“/emp”)</td>
</tr>
</tbody></table>
<p> 针对Http的八种请求方式都有专门的注解</p>
<p> 另外需要注意：进阶版的这几个注解是从 4.3 版本才开始有，低于 4.3 版本无法使用。 </p>
<h5 id="1-4-Ambiguous-mapping异常"><a href="#1-4-Ambiguous-mapping异常" class="headerlink" title="1.4 Ambiguous mapping异常"></a>1.4 Ambiguous mapping异常</h5><p>出现原因：多个 handler 方法映射了同一个地址，导致 SpringMVC 在接收到这个地址的请求时该找哪个 handler 方法处理。</p>
<blockquote>
<p>Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘demo03MappingMethodHandler’ method  com.atguigu.mvc.handler.Demo03MappingMethodHandler#empPost() to { [/emp]}: <strong>There is already</strong> ‘demo03MappingMethodHandler’ bean method com.atguigu.mvc.handler.Demo03MappingMethodHandler#empGet() <strong>mapped</strong>.</p>
</blockquote>
<h4 id="2-RequestHeader注解-了解"><a href="#2-RequestHeader注解-了解" class="headerlink" title="2. RequestHeader注解(了解)"></a>2. RequestHeader注解(了解)</h4><h5 id="2-1-作用"><a href="#2-1-作用" class="headerlink" title="2.1 作用"></a>2.1 作用</h5><p>通过这个注解获取请求消息头中的具体数据。 </p>
<h5 id="2-2-用法"><a href="#2-2-用法" class="headerlink" title="2.2 用法"></a>2.2 用法</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/getHeader&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getHeaderValue</span><span class="hljs-params">(<span class="hljs-meta">@RequestHeader(&quot;user-agent&quot;)</span> String headerValue)</span></span>&#123;<br>    <span class="hljs-comment">//目标:获取user-agent请求头的信息，并且赋值给headerValue</span><br>    logger.debug(headerValue);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="3-CookieValue注解-了解"><a href="#3-CookieValue注解-了解" class="headerlink" title="3. CookieValue注解(了解)"></a>3. CookieValue注解(了解)</h4><h5 id="3-1-作用"><a href="#3-1-作用" class="headerlink" title="3.1 作用"></a>3.1 作用</h5><p>获取当前请求中的 Cookie 数据。 </p>
<h5 id="3-2-用法"><a href="#3-2-用法" class="headerlink" title="3.2 用法"></a>3.2 用法</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/getCookie&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getCookieValue</span><span class="hljs-params">(<span class="hljs-meta">@CookieValue(value = &quot;JSESSIONID&quot;,defaultValue = &quot;abc&quot;)</span> String cookieValue)</span></span>&#123;<br>    <span class="hljs-comment">//目标:获取浏览器中的名为&quot;JSESSIONID&quot;的cookie的值，赋值给CookieValue参数</span><br>    logger.debug(cookieValue);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第四节-获取请求参数-重点"><a href="#第四节-获取请求参数-重点" class="headerlink" title="第四节 获取请求参数(重点)"></a>第四节 获取请求参数(重点)</h3><h4 id="1-根据一个参数名获取一个参数值"><a href="#1-根据一个参数名获取一个参数值" class="headerlink" title="1. 根据一个参数名获取一个参数值"></a>1. 根据一个参数名获取一个参数值</h4><h5 id="1-1-前端页面携带请求参数"><a href="#1-1-前端页面携带请求参数" class="headerlink" title="1.1 前端页面携带请求参数"></a>1.1 前端页面携带请求参数</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/param/one(userName=&#x27;tom&#x27;)&#125;&quot;</span>&gt;</span>一个名字一个值的情况<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-2-处理器中获取请求参数"><a href="#1-2-处理器中获取请求参数" class="headerlink" title="1.2 处理器中获取请求参数"></a>1.2 处理器中获取请求参数</h5><h6 id="1-2-1-最简写法"><a href="#1-2-1-最简写法" class="headerlink" title="1.2.1 最简写法"></a>1.2.1 最简写法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/oneParameter&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">oneParameter</span><span class="hljs-params">(String username)</span></span>&#123;<br>    <span class="hljs-comment">//最简单的方式获取单个请求参数:就是在handler方法中添加一个和请求参数名同名的参数，来接收请求参数</span><br>    <span class="hljs-comment">//其实这个地方String username此处省略了一个注解 @RequestParam</span><br>    <span class="hljs-comment">//@RequestParam(&quot;username&quot;)就是通过参数名获取请求参数</span><br>    logger.debug(username);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-2-2-使用RequestParam注解"><a href="#1-2-2-使用RequestParam注解" class="headerlink" title="1.2.2 使用RequestParam注解"></a>1.2.2 使用RequestParam注解</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/oneParameter&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">oneParameter</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam(&quot;username&quot;)</span> String username)</span></span>&#123;<br>    <span class="hljs-comment">//最简单的方式获取单个请求参数:就是在handler方法中添加一个和请求参数名同名的参数，来接收请求参数</span><br>    <span class="hljs-comment">//其实这个地方String username此处省略了一个注解 @RequestParam</span><br>    <span class="hljs-comment">//@RequestParam(&quot;username&quot;)就是通过参数名获取请求参数</span><br>    logger.debug(username);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="1-2-3-如果此时请求中没有携带username参数"><a href="#1-2-3-如果此时请求中没有携带username参数" class="headerlink" title="1.2.3 如果此时请求中没有携带username参数"></a>1.2.3 如果此时请求中没有携带username参数</h6><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_007.png" srcset="/img/loading.gif" lazyload></p>
<p>页面信息说明：</p>
<ul>
<li>响应状态码：400（在 SpringMVC 环境下，400通常和数据注入相关）</li>
<li>说明信息：必需的 String 请求参数 ‘username’ 不存在</li>
</ul>
<p>原因可以参考 @RequestParam 注解的 required 属性：默认值为true，表示请求参数默认必须提供</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">     * Whether the parameter is required.</span><br><span class="hljs-comment">     * &lt;p&gt;Defaults to &#123;<span class="hljs-doctag">@code</span> true&#125;, leading to an exception being thrown</span><br><span class="hljs-comment">     * if the parameter is missing in the request. Switch this to</span><br><span class="hljs-comment">     * &#123;<span class="hljs-doctag">@code</span> false&#125; if you prefer a &#123;<span class="hljs-doctag">@code</span> null&#125; value if the parameter is</span><br><span class="hljs-comment">     * not present in the request.</span><br><span class="hljs-comment">     * &lt;p&gt;Alternatively, provide a &#123;<span class="hljs-doctag">@link</span> #defaultValue&#125;, which implicitly</span><br><span class="hljs-comment">     * sets this flag to &#123;<span class="hljs-doctag">@code</span> false&#125;.</span><br><span class="hljs-comment">     */</span><br><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">required</span><span class="hljs-params">()</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">true</span></span>;<br></code></pre></td></tr></table></figure>

<p>我们可以通过将required 属性设置为 false 表示这个请求参数可有可无： </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestParam(value = &quot;userName&quot;, required = false)</span><br></code></pre></td></tr></table></figure>
<p>当然，我们也可以通过设置请求参数的默认值来解决上述<code>400</code>错误</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestParam(value = &quot;userName&quot;, defaultValue = &quot;missing&quot;)</span><br></code></pre></td></tr></table></figure>

<h4 id="2-根据一个参数名获取多个参数值"><a href="#2-根据一个参数名获取多个参数值" class="headerlink" title="2. 根据一个参数名获取多个参数值"></a>2. 根据一个参数名获取多个参数值</h4><h5 id="2-1-前端页面携带请求参数"><a href="#2-1-前端页面携带请求参数" class="headerlink" title="2.1 前端页面携带请求参数"></a>2.1 前端页面携带请求参数</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">th:action</span>=<span class="hljs-string">&quot;@&#123;/param/multi&#125;&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br>    请选择你最喜欢的球队：<br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;team&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;Brazil&quot;</span>/&gt;</span>巴西<br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;team&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;German&quot;</span>/&gt;</span>德国<br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;team&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;French&quot;</span>/&gt;</span>法国<br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;team&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;Holland&quot;</span>/&gt;</span>荷兰<br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;team&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;Italian&quot;</span>/&gt;</span>意大利<br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;checkbox&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;team&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;China&quot;</span>/&gt;</span>中国<br>    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;保存&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-2-处理器中获取请求参数"><a href="#2-2-处理器中获取请求参数" class="headerlink" title="2.2 处理器中获取请求参数"></a>2.2 处理器中获取请求参数</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/multi&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">oneNameMultiValue</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam(&quot;team&quot;)</span>List&lt;String&gt; teamList)</span></span>&#123;<br>    logger.debug(teamList.toString());<br>    <span class="hljs-comment">//要使用什么类型来接收参数</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="3-使用实体类封装请求参数"><a href="#3-使用实体类封装请求参数" class="headerlink" title="3. 使用实体类封装请求参数"></a>3. 使用实体类封装请求参数</h4><h5 id="3-1-前端页面携带请求参数"><a href="#3-1-前端页面携带请求参数" class="headerlink" title="3.1 前端页面携带请求参数"></a>3.1 前端页面携带请求参数</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">th:action</span>=<span class="hljs-string">&quot;emp/save&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br>    姓名：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;empName&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    年龄：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;empAge&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    工资：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;empSalary&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;保存&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="3-2-封装实体类"><a href="#3-2-封装实体类" class="headerlink" title="3.2 封装实体类"></a>3.2 封装实体类</h5><p>注意: 实体类的属性名要和请求参数名一致</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Employee</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String empName;<br>    <span class="hljs-keyword">private</span> Integer empAge;<br>    <span class="hljs-keyword">private</span> Double empSalary;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-3-处理器中获取请求参数"><a href="#3-3-处理器中获取请求参数" class="headerlink" title="3.3 处理器中获取请求参数"></a>3.3 处理器中获取请求参数</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/pojo&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">parameterToPojo</span><span class="hljs-params">(Employee employee)</span></span>&#123;<br>    <span class="hljs-comment">//获取请求参数封装到Employee对象中:只需要保证Employee类中的属性和请求参数名一致</span><br>    logger.debug(employee.toString());<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-4-解决POST请求的中文字符乱码问题"><a href="#3-4-解决POST请求的中文字符乱码问题" class="headerlink" title="3.4 解决POST请求的中文字符乱码问题"></a>3.4 解决POST请求的中文字符乱码问题</h5><p>到 web.xml 中配置 CharacterEncodingFilter 即可：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 配置过滤器解决 POST 请求的字符乱码问题 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">filter</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">filter-class</span>&gt;</span>org.springframework.web.filter.CharacterEncodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-class</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- encoding参数指定要使用的字符集名称 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>encoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- 请求强制编码 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>forceRequestEncoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 响应强制编码 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>forceResponseEncoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">filter-mapping</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/*<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">filter-mapping</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>注1：在较低版本的 SpringMVC 中，forceRequestEncoding 属性、forceResponseEncoding 属性没有分开，它们是一个 forceEncoding 属性。这里需要注意一下。</p>
<p>注2：由于 CharacterEncodingFilter 是通过 request.setCharacterEncoding(encoding); 来设置请求字符集，所以在此操作前不能有任何的 request.getParameter() 操作。在设置字符集之前获取过请求参数，那么设置字符集的操作将无效。</p>
<h4 id="4-使用Map封装请求参数"><a href="#4-使用Map封装请求参数" class="headerlink" title="4. 使用Map封装请求参数"></a>4. 使用Map封装请求参数</h4><h5 id="4-1-前端页面表单"><a href="#4-1-前端页面表单" class="headerlink" title="4.1 前端页面表单"></a>4.1 前端页面表单</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">th:action</span>=<span class="hljs-string">&quot;@&#123;/parameter/map&#125;&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br>    姓名：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;empName&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    年龄：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;empAge&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    工资：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;empSalary&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;保存&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="4-2-handler方法接收参数"><a href="#4-2-handler方法接收参数" class="headerlink" title="4.2 handler方法接收参数"></a>4.2 handler方法接收参数</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/map&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">parameterToMap</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam</span> Map map)</span></span>&#123;<br>    <span class="hljs-comment">//获取请求参数，封装到Map中:请求参数名就是map的key，请求参数值就是map的value</span><br>    logger.debug(map.toString());<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="5-实体类封装的数据包含级联属性-了解"><a href="#5-实体类封装的数据包含级联属性-了解" class="headerlink" title="5. 实体类封装的数据包含级联属性(了解"></a>5. 实体类封装的数据包含级联属性(了解</h4><h5 id="5-1-实体类"><a href="#5-1-实体类" class="headerlink" title="5.1 实体类"></a>5.1 实体类</h5><h6 id="Student"><a href="#Student" class="headerlink" title="Student"></a>Student</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String stuName;<br>    <span class="hljs-keyword">private</span> School school;<br>    <span class="hljs-keyword">private</span> List&lt;Subject&gt; subjectList;<br>    <span class="hljs-keyword">private</span> Subject[] subjectArray;<br>    <span class="hljs-keyword">private</span> List&lt;Teacher&gt; teacherList;<br>    <span class="hljs-keyword">private</span> Map&lt;String, Double&gt; scores;<br>    ……<br></code></pre></td></tr></table></figure>

<h6 id="School"><a href="#School" class="headerlink" title="School"></a>School</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">School</span> </span>&#123;<br>    <br>    <span class="hljs-keyword">private</span> String schoolName;<br>    ……<br></code></pre></td></tr></table></figure>

<h6 id="Subject"><a href="#Subject" class="headerlink" title="Subject"></a>Subject</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Subject</span> </span>&#123;<br>    <br>    <span class="hljs-keyword">private</span> String subjectName;<br>    ……<br></code></pre></td></tr></table></figure>

<h6 id="Teacher"><a href="#Teacher" class="headerlink" title="Teacher"></a>Teacher</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Teacher</span> </span>&#123;<br>    <br>    <span class="hljs-keyword">private</span> String teacherName;<br>    ……<br></code></pre></td></tr></table></figure>

<h5 id="5-2-前端页面携带请求参数"><a href="#5-2-前端页面携带请求参数" class="headerlink" title="5.2 前端页面携带请求参数"></a>5.2 前端页面携带请求参数</h5><p>表单项中的 name 属性值必须严格按照级联对象的属性来设定： </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 提交数据的表单 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">th:action</span>=<span class="hljs-string">&quot;@&#123;param/toNestEntity&#125;&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br>    stuName：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;stuName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;tom&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    school.schoolName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;school.schoolName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;atguigu&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectList[0].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectList[0].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;java&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectList[1].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectList[1].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;php&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectList[2].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectList[2].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;javascript&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectList[3].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectList[3].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;css&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectList[4].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectList[4].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;vue&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectArray[0].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectArray[0].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;spring&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectArray[1].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectArray[1].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;SpringMVC&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectArray[2].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectArray[2].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;mybatis&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectArray[3].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectArray[3].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;maven&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    subjectArray[4].subjectName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subjectArray[4].subjectName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;mysql&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    teacherList[0].teacherName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teacherList[0].teacherName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;t_one&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    teacherList[1].teacherName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teacherList[1].teacherName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;t_two&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    teacherList[2].teacherName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teacherList[2].teacherName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;t_three&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    teacherList[3].teacherName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teacherList[3].teacherName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;t_four&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    teacherList[4].teacherName:<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;teacherList[4].teacherName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;t_five&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    scores[&#x27;Chinese&#x27;]：input type=&quot;text&quot; name=&quot;scores[&#x27;Chinese&#x27;]&quot; value=&quot;100&quot;/&gt;<span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    scores[&#x27;English&#x27;]：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;scores[&#x27;English&#x27;]&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;95&quot;</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    scores[&#x27;Mathematics&#x27;]：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;scores[&#x27;Mathematics&#x27;]&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;88&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    scores[&#x27;Chemistry&#x27;]：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;scores[&#x27;Chemistry&#x27;]&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;63&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    scores[&#x27;Biology&#x27;]：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;scores[&#x27;Biology&#x27;]&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;44&quot;</span>/&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;保存&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="5-3-处理器中获取请求参数"><a href="#5-3-处理器中获取请求参数" class="headerlink" title="5.3 处理器中获取请求参数"></a>5.3 处理器中获取请求参数</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/param/toNestEntity&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">formToNestedEntity</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function">        // SpringMVC 自己懂得注入级联属性，只要属性名和对应的getXxx()</span>、<span class="hljs-title">setXxx</span><span class="hljs-params">()</span>匹配即可</span><br><span class="hljs-function">        Student student) </span>&#123;<br>    logger.debug(student.toString());<br>    <br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第五节-访问静态资源-重点"><a href="#第五节-访问静态资源-重点" class="headerlink" title="第五节 访问静态资源(重点)"></a>第五节 访问静态资源(重点)</h3><h4 id="1-SpringMVC环境下的静态资源访问问题"><a href="#1-SpringMVC环境下的静态资源访问问题" class="headerlink" title="1. SpringMVC环境下的静态资源访问问题"></a>1. SpringMVC环境下的静态资源访问问题</h4><h5 id="1-1-问题描述"><a href="#1-1-问题描述" class="headerlink" title="1.1 问题描述"></a>1.1 问题描述</h5><p>在 Web 应用中加入图片资源：</p>
<p> <img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_009.png" srcset="/img/loading.gif" lazyload></p>
<p>访问静态资源</p>
<p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_010.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="1-2-分析原因"><a href="#1-2-分析原因" class="headerlink" title="1.2 分析原因"></a>1.2 分析原因</h5><ul>
<li>DispatcherServlet 的 url-pattern 配置的是“/”</li>
<li>url-pattern 配置“/”表示整个 Web 应用范围内所有请求都由 SpringMVC 来处理</li>
<li>对 SpringMVC 来说，必须有对应的 @RequestMapping 才能找到处理请求的方法</li>
<li>现在static/images/mm.jpg 请求没有对应的 @RequestMapping 所以返回 404</li>
</ul>
<h5 id="1-3-解决办法"><a href="#1-3-解决办法" class="headerlink" title="1.3 解决办法"></a>1.3 解决办法</h5><p>在 SpringMVC 配置文件中增加配置：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 加入这个配置，SpringMVC 就会在遇到没有 @RequestMapping 的请求时放它过去 --&gt;</span><br><span class="hljs-comment">&lt;!-- 所谓放它过去就是让这个请求去找它原本要访问的资源 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mvc:default-servlet-handler</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<p>再次测试访问图片：</p>
<p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_011.png" srcset="/img/loading.gif" lazyload></p>
<p>新的问题：其他原本正常的请求访问不了了,进一步解决问题：再增加一个配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 开启 SpringMVC 的注解驱动功能。这个配置也被称为 SpringMVC 的标配。 --&gt;</span><br><span class="hljs-comment">&lt;!-- 标配：因为 SpringMVC 环境下非常多的功能都要求必须打开注解驱动才能正常工作。 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mvc:annotation-driven</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-4-default-servlet-handler底层-了解"><a href="#1-4-default-servlet-handler底层-了解" class="headerlink" title="1.4 default-servlet-handler底层[了解]"></a>1.4 default-servlet-handler底层[了解]</h5><p>所在类：org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler</p>
<p>关键方法：handleRequest()方法</p>
<p>大体机制：SpringMVC 首先查找是否存在和当前请求对应的 @RequestMapping；如果没有，则调用handleRequest()方法转发到目标资源。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Override</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleRequest</span><span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)</span></span><br><span class="hljs-function">            <span class="hljs-keyword">throws</span> ServletException, IOException </span>&#123;<br>    <br>    Assert.state(<span class="hljs-keyword">this</span>.servletContext != <span class="hljs-keyword">null</span>, <span class="hljs-string">&quot;No ServletContext set&quot;</span>);<br>    RequestDispatcher rd = <span class="hljs-keyword">this</span>.servletContext.getNamedDispatcher(<span class="hljs-keyword">this</span>.defaultServletName);<br>    <span class="hljs-keyword">if</span> (rd == <span class="hljs-keyword">null</span>) &#123;<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalStateException(<span class="hljs-string">&quot;A RequestDispatcher could not be located for the default servlet &#x27;&quot;</span> +<br>        <span class="hljs-keyword">this</span>.defaultServletName + <span class="hljs-string">&quot;&#x27;&quot;</span>);<br>    &#125;<br>    <br>    <span class="hljs-comment">// 这里执行请求转发操作</span><br>    rd.forward(request, response);<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第六节-页面跳转控制"><a href="#第六节-页面跳转控制" class="headerlink" title="第六节 页面跳转控制"></a>第六节 页面跳转控制</h3><h4 id="1-访问Thymeleaf前后缀控制范围内的页面"><a href="#1-访问Thymeleaf前后缀控制范围内的页面" class="headerlink" title="1. 访问Thymeleaf前后缀控制范围内的页面"></a>1. 访问Thymeleaf前后缀控制范围内的页面</h4><p>访问Thymeleaf前后缀控制范围之内的页面，直接<code>return</code>逻辑视图即可，但是访问前后缀控制范围之外的页面，则需要使用重定向或者请求转发</p>
<h4 id="2-访问Thymeleaf前后缀控制范围外的页面-了解"><a href="#2-访问Thymeleaf前后缀控制范围外的页面-了解" class="headerlink" title="2. 访问Thymeleaf前后缀控制范围外的页面(了解)"></a>2. 访问Thymeleaf前后缀控制范围外的页面(了解)</h4><h5 id="2-1-创建范围之外的页面"><a href="#2-1-创建范围之外的页面" class="headerlink" title="2.1 创建范围之外的页面"></a>2.1 创建范围之外的页面</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_008.png" srcset="/img/loading.gif" lazyload></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <br>    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>范围之外页面<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>    <br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-2-在-SpringMVC-配置文件加入配置"><a href="#2-2-在-SpringMVC-配置文件加入配置" class="headerlink" title="2.2 在 SpringMVC 配置文件加入配置"></a>2.2 在 SpringMVC 配置文件加入配置</h5><p>下面配置是访问静态资源所需配置(如果没有它的话，访问静态资源会被DispatcherServlet所拦截)： </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">mvc:annotation-driven</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mvc:default-servlet-handler</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-3-使用指令进行跳转-了解"><a href="#2-3-使用指令进行跳转-了解" class="headerlink" title="2.3 使用指令进行跳转(了解)"></a>2.3 使用指令进行跳转(了解)</h5><h6 id="2-3-1-请求转发的指令"><a href="#2-3-1-请求转发的指令" class="headerlink" title="2.3.1 请求转发的指令"></a>2.3.1 请求转发的指令</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/forwardCommand&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">forwardCommand</span><span class="hljs-params">()</span></span>&#123;<br>    <span class="hljs-comment">//使用转发指令:在handler方法中，访问Thymeleaf前后缀控制范围外的页面</span><br>    <span class="hljs-comment">//&quot;forward:要转发到的资源路径&quot;,相当于使用的是请求转发跳转</span><br>    <span class="hljs-comment">//请求转发的绝对路径是:在uri的基础上省略&quot;/项目名&quot;</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;forward:/outter.html&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="2-3-2-重定向的指令"><a href="#2-3-2-重定向的指令" class="headerlink" title="2.3.2 重定向的指令"></a>2.3.2 重定向的指令</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/redirectCommand&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">redirectCommand</span><span class="hljs-params">()</span></span>&#123;<br>    <span class="hljs-comment">//使用重定向指令:在handler方法中，访问Thymeleaf前后缀控制范围外的页面</span><br>    <span class="hljs-comment">//&quot;redirect:要转发到的资源路径&quot;,相当于使用的是请求转发跳转</span><br>    <span class="hljs-comment">//重定向的绝对路径:就是uri   &quot;/项目名/资源路径&quot;</span><br>    <span class="hljs-comment">//因为SpringMVC框架在接收到redirect指令之后，会自动给访问路径加上&quot;/项目名&quot;</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;redirect:/outter.html&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="3-mvc-view-controller访问页面-重点"><a href="#3-mvc-view-controller访问页面-重点" class="headerlink" title="3. mvc:view-controller访问页面(重点)"></a>3. mvc:view-controller访问页面(重点)</h4><h5 id="3-1-场景描述"><a href="#3-1-场景描述" class="headerlink" title="3.1 场景描述"></a>3.1 场景描述</h5><p>在一个 handler 方法中，仅仅只是完成 @RequestMapping 映射，将请求转发到目标视图，除此之外没有任何其他代码。此时可以使用 SpringMVC 配置文件中的配置代替这样的 handler 方法。 </p>
<h5 id="3-2-具体操作"><a href="#3-2-具体操作" class="headerlink" title="3.2 具体操作"></a>3.2 具体操作</h5><h6 id="3-2-1-访问Thymeleaf前后缀控制范围内的页面"><a href="#3-2-1-访问Thymeleaf前后缀控制范围内的页面" class="headerlink" title="3.2.1 访问Thymeleaf前后缀控制范围内的页面"></a>3.2.1 访问Thymeleaf前后缀控制范围内的页面</h6><p>在 SpringMVC 配置文件中使用 mvc:view-controller 配置： </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">mvc:view-controller</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/index.html&quot;</span> <span class="hljs-attr">view-name</span>=<span class="hljs-string">&quot;portal&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<p>此时浏览器路径”index.html”可以访问到Thymeleaf前后缀控制范围内的portal.html页面</p>
<h6 id="3-2-2-访问Thymeleaf前后缀控制范围外的页面"><a href="#3-2-2-访问Thymeleaf前后缀控制范围外的页面" class="headerlink" title="3.2.2 访问Thymeleaf前后缀控制范围外的页面"></a>3.2.2 访问Thymeleaf前后缀控制范围外的页面</h6><p>在 SpringMVC 配置文件中使用 mvc:view-controller 配置：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">mvc:view-controller</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/test/redirect/command&quot;</span> <span class="hljs-attr">view-name</span>=<span class="hljs-string">&quot;redirect:/outter.html&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<p>此时就相当于代替了之前的重定向访问的那个handler方法</p>
<h5 id="3-3-遇到的问题"><a href="#3-3-遇到的问题" class="headerlink" title="3.3 遇到的问题"></a>3.3 遇到的问题</h5><p>加入 mvc:view-controller 配置后，其他正常 @RequestMapping 将失效。此时还是需要加入 mvc:annotation-driven 来解决。 </p>
<h4 id="4-三个配置影响访问效果探索-了解"><a href="#4-三个配置影响访问效果探索-了解" class="headerlink" title="4. 三个配置影响访问效果探索(了解)"></a>4. 三个配置影响访问效果探索(了解)</h4><p>通过前面的学习，我们发现无论是添加了<code>default-servlet-handler</code>还是<code>view-controller</code>,我们都必须添加<code>annotation-driven</code>标签。所以<code>annotation-driven</code>标签是SpringMVC必须添加的</p>
<p>接下来我们从源码角度分析这三个配置:</p>
<h5 id="4-1-相关组件：HandlerMapping"><a href="#4-1-相关组件：HandlerMapping" class="headerlink" title="4.1 相关组件：HandlerMapping"></a>4.1 相关组件：HandlerMapping</h5><p>见名知意，HandlerMapping 封装的数据包含了请求地址和 handler 方法之间的映射关系。所以请求访问是否能生效关键要看 HandlerMapping 在 IOC 容器中加载的情况。为了看到这一点，我们可以在 DispatcherServlet 中找到 doDispatch() 方法设置断点。之所以选择这个方法，是因为每一个由 SpringMVC 处理的请求都会经过这里，便于操作。 </p>
<p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_014.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="4-2-按三种标签的配置情况进行分析"><a href="#4-2-按三种标签的配置情况进行分析" class="headerlink" title="4.2 按三种标签的配置情况进行分析"></a>4.2 按三种标签的配置情况进行分析</h5><h6 id="4-2-1-三个标签都没有配置的情况"><a href="#4-2-1-三个标签都没有配置的情况" class="headerlink" title="4.2.1 三个标签都没有配置的情况"></a>4.2.1 三个标签都没有配置的情况</h6><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_015.png" srcset="/img/loading.gif" lazyload></p>
<p>我们看到 SpringMVC 加载了三个 HandlerMapping：</p>
<blockquote>
<p>org.springframework.web.servlet.handler.<strong>BeanNameUrlHandlerMapping</strong></p>
<p>org.springframework.web.servlet.mvc.method.annotation.<strong>RequestMappingHandlerMapping</strong></p>
<p>org.springframework.web.servlet.function.support.<strong>RouterFunctionMapping</strong></p>
</blockquote>
<p>其中 RequestMappingHandlerMapping 封装了 @RequestMapping 相关请求，有它在 @RequestMapping 相关请求就能访问到。</p>
<p>这里顺带一提，在较低版本的 SpringMVC 此处要加载的是：</p>
<blockquote>
<p>org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping</p>
</blockquote>
<p>其中 DefaultAnnotationHandlerMapping 封装了 @RequestMapping 相关请求，有它在 @RequestMapping 相关请求就能访问到。</p>
<h6 id="4-2-2-配置了-mvc-view-controller-或-mvc-default-servlet-handler-的情况"><a href="#4-2-2-配置了-mvc-view-controller-或-mvc-default-servlet-handler-的情况" class="headerlink" title="4.2.2  配置了 mvc:view-controller 或 mvc:default-servlet-handler 的情况"></a>4.2.2  配置了 mvc:view-controller 或 mvc:default-servlet-handler 的情况</h6><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_016.png" srcset="/img/loading.gif" lazyload></p>
<p>我们看到 SpringMVC 加载了两个 HandlerMapping：</p>
<blockquote>
<p>org.springframework.web.servlet.handler.<strong>SimpleUrlHandlerMapping</strong></p>
<p>org.springframework.web.servlet.handler.<strong>BeanNameUrlHandlerMapping</strong></p>
</blockquote>
<p>此时没有了RequestMappingHandlerMapping，所以无法根据@RequestMapping的映射来调用handler方法了</p>
<p>较低版本的 SpringMVC 在这里的情况一样。</p>
<h6 id="4-2-3-三个标签全部配置了的情况"><a href="#4-2-3-三个标签全部配置了的情况" class="headerlink" title="4.2.3 三个标签全部配置了的情况"></a>4.2.3 三个标签全部配置了的情况</h6><p>配置全部 mvc:view-controller、mvc:default-servlet-handler、mvc:annotation-driven 三个标签。 </p>
<p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_017.png" srcset="/img/loading.gif" lazyload></p>
<p>我们看到 SpringMVC 加载了略有不同的三个 HandlerMapping：</p>
<blockquote>
<p>org.springframework.web.servlet.mvc.method.annotation.<strong>RequestMappingHandlerMapping</strong></p>
<p>org.springframework.web.servlet.handler.<strong>SimpleUrlHandlerMapping</strong></p>
<p>org.springframework.web.servlet.handler.<strong>BeanNameUrlHandlerMapping</strong></p>
</blockquote>
<p>此时RequestMappingHandlerMapping 封装了 @RequestMapping 相关请求，有它在 @RequestMapping 相关请求就能访问到。</p>
<p>较低版本的 SpringMVC 在这里的情况还是一样。</p>
<h2 id="第二章-SpringMVC使用域对象"><a href="#第二章-SpringMVC使用域对象" class="headerlink" title="第二章 SpringMVC使用域对象"></a>第二章 SpringMVC使用域对象</h2><h3 id="第一节-请求域对象"><a href="#第一节-请求域对象" class="headerlink" title="第一节 请求域对象"></a>第一节 请求域对象</h3><h4 id="1-将数据存入请求域对象"><a href="#1-将数据存入请求域对象" class="headerlink" title="1. 将数据存入请求域对象"></a>1. 将数据存入请求域对象</h4><h5 id="1-1-使用原生的Servlet的API操作"><a href="#1-1-使用原生的Servlet的API操作" class="headerlink" title="1.1 使用原生的Servlet的API操作"></a>1.1 使用原生的Servlet的API操作</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/testAttrOriginalRequest&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">testAttrOriginalRequest</span><span class="hljs-params">(HttpServletRequest request)</span></span>&#123;<br>    <span class="hljs-comment">//目标:将数据存储到请求域对象，然后跳转到target页面</span><br>    <span class="hljs-comment">//方式一:使用原始的request来完成</span><br>    request.setAttribute(<span class="hljs-string">&quot;username&quot;</span>,<span class="hljs-string">&quot;aobama&quot;</span>);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-2-使用-Model-类型的形参操作"><a href="#1-2-使用-Model-类型的形参操作" class="headerlink" title="1.2 使用 Model 类型的形参操作"></a>1.2 使用 Model 类型的形参操作</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/testAttrRequestModel&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">testAttrRequestModel</span><span class="hljs-params">(Model model)</span></span>&#123;<br>    <span class="hljs-comment">//目标:目标:将数据存储到请求域对象，然后跳转到target页面</span><br>    <span class="hljs-comment">//方式二:使用Model对象往请求域中存储值</span><br>    model.addAttribute(<span class="hljs-string">&quot;username&quot;</span>,<span class="hljs-string">&quot;aolafu&quot;</span>);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-3-使用-ModelMap-类型的形参"><a href="#1-3-使用-ModelMap-类型的形参" class="headerlink" title="1.3 使用 ModelMap 类型的形参"></a>1.3 使用 ModelMap 类型的形参</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/attr/request/model/map&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">testAttrRequestModelMap</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function">    </span></span><br><span class="hljs-params"><span class="hljs-function">        // 在形参位置声明ModelMap类型变量，用于存储模型数据</span></span><br><span class="hljs-params"><span class="hljs-function">        ModelMap modelMap)</span> </span>&#123;<br>    <br>    <span class="hljs-comment">// 我们将数据存入模型，SpringMVC 会帮我们把模型数据存入请求域</span><br>    <span class="hljs-comment">// 存入请求域这个动作也被称为暴露到请求域</span><br>    modelMap.addAttribute(<span class="hljs-string">&quot;requestScopeMessageModelMap&quot;</span>,<span class="hljs-string">&quot;i am very happy[model map]&quot;</span>);<br>    <br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-4-使用-Map-类型的形参"><a href="#1-4-使用-Map-类型的形参" class="headerlink" title="1.4 使用 Map 类型的形参"></a>1.4 使用 Map 类型的形参</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/attr/request/map&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">testAttrRequestMap</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function">    </span></span><br><span class="hljs-params"><span class="hljs-function">        // 在形参位置声明Map类型变量，用于存储模型数据</span></span><br><span class="hljs-params"><span class="hljs-function">        Map&lt;String, Object&gt; map)</span> </span>&#123;<br>    <br>    <span class="hljs-comment">// 我们将数据存入模型，SpringMVC 会帮我们把模型数据存入请求域</span><br>    <span class="hljs-comment">// 存入请求域这个动作也被称为暴露到请求域</span><br>    map.put(<span class="hljs-string">&quot;requestScopeMessageMap&quot;</span>, <span class="hljs-string">&quot;i am very happy[map]&quot;</span>);<br>    <br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-5-使用-ModelAndView-对象"><a href="#1-5-使用-ModelAndView-对象" class="headerlink" title="1.5 使用 ModelAndView 对象"></a>1.5 使用 ModelAndView 对象</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/attr/request/mav&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> ModelAndView <span class="hljs-title">testAttrByModelAndView</span><span class="hljs-params">()</span> </span>&#123;<br>    <br>    <span class="hljs-comment">// 1.创建ModelAndView对象</span><br>    ModelAndView modelAndView = <span class="hljs-keyword">new</span> ModelAndView();<br>    <br>    <span class="hljs-comment">// 2.存入模型数据</span><br>    modelAndView.addObject(<span class="hljs-string">&quot;requestScopeMessageMAV&quot;</span>, <span class="hljs-string">&quot;i am very happy[mav]&quot;</span>);<br>    <br>    <span class="hljs-comment">// 3.设置视图名称</span><br>    modelAndView.setViewName(<span class="hljs-string">&quot;target&quot;</span>);<br>    <br>    <span class="hljs-keyword">return</span> modelAndView;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="2-模型的本质"><a href="#2-模型的本质" class="headerlink" title="2. 模型的本质"></a>2. 模型的本质</h4><h5 id="2-1-BindingAwareModelMap"><a href="#2-1-BindingAwareModelMap" class="headerlink" title="2.1 BindingAwareModelMap"></a>2.1 BindingAwareModelMap</h5><p>SpringMVC 传入的 Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的。 </p>
<h5 id="2-2-体系结构"><a href="#2-2-体系结构" class="headerlink" title="2.2 体系结构"></a>2.2 体系结构</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_012.png" srcset="/img/loading.gif" lazyload></p>
<h4 id="3-框架底层将模型存入请求域的原理"><a href="#3-框架底层将模型存入请求域的原理" class="headerlink" title="3. 框架底层将模型存入请求域的原理"></a>3. 框架底层将模型存入请求域的原理</h4><h5 id="3-1-源码位置"><a href="#3-1-源码位置" class="headerlink" title="3.1 源码位置"></a>3.1 源码位置</h5><p>所在类：org.thymeleaf.context.WebEngineContext</p>
<p>所在方法：setVariable()</p>
<p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_013.png" srcset="/img/loading.gif" lazyload></p>
<h3 id="第二节-会话域对象"><a href="#第二节-会话域对象" class="headerlink" title="第二节 会话域对象"></a>第二节 会话域对象</h3><p>使用会话域最简单直接的办法就是使用原生的 HttpSession 对象 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/attrSession&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">attrSession</span><span class="hljs-params">(HttpSession httpSession)</span></span>&#123;<br>    <span class="hljs-comment">//目标:往会话域中存储数据</span><br>    httpSession.setAttribute(<span class="hljs-string">&quot;address&quot;</span>,<span class="hljs-string">&quot;召唤师峡谷&quot;</span>);<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第三节-应用域"><a href="#第三节-应用域" class="headerlink" title="第三节 应用域"></a>第三节 应用域</h3><p>应用域同样是使用IOC注入的方式来操作： </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Autowired</span><br><span class="hljs-keyword">private</span> ServletContext servletContext;<br><span class="hljs-meta">@RequestMapping(&quot;/attr/application&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">attrApplication</span><span class="hljs-params">()</span> </span>&#123;<br>    <br>    servletContext.setAttribute(<span class="hljs-string">&quot;appScopeMsg&quot;</span>, <span class="hljs-string">&quot;i am hungry...&quot;</span>);<br>    <br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;target&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="第三章-综合案例"><a href="#第三章-综合案例" class="headerlink" title="第三章 综合案例"></a>第三章 综合案例</h2><h3 id="第一节-案例准备工作"><a href="#第一节-案例准备工作" class="headerlink" title="第一节 案例准备工作"></a>第一节 案例准备工作</h3><h4 id="1-引入依赖"><a href="#1-引入依赖" class="headerlink" title="1. 引入依赖"></a>1. 引入依赖</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- SpringMVC --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-webmvc<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.3.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- 日志 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>ch.qos.logback<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>logback-classic<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.2.3<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- ServletAPI --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>javax.servlet<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>javax.servlet-api<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.1.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>provided<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- Spring5和Thymeleaf整合包 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.thymeleaf<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>thymeleaf-spring5<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>3.0.12.RELEASE<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.junit.jupiter<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>junit-jupiter-api<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.7.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.3.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--引入LomBok的依赖--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.18.8<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>provided<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 导入 orm 包就可以通过 Maven 的依赖传递性把其他两个也导入 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-orm<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.3.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><br>    <span class="hljs-comment">&lt;!-- MySQL驱动 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>mysql<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.1.3<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 数据源 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>druid<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0.31<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="2-数据建模"><a href="#2-数据建模" class="headerlink" title="2 数据建模"></a>2 数据建模</h4><h5 id="2-1-物理建模"><a href="#2-1-物理建模" class="headerlink" title="2.1 物理建模"></a>2.1 物理建模</h5><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs mysql">CREATE TABLE t_movie(<br>	movie_id INT PRIMARY KEY AUTO_INCREMENT,<br>	movie_name VARCHAR(50),<br>	movie_price DOUBLE<br>);<br>INSERT INTO t_movie(movie_name,movie_price) <br>VALUES (&quot;肖申克救赎&quot;,10.0),<br>(&quot;泰坦尼克号&quot;,20.0),<br>(&quot;审死官&quot;,30.0),<br>(&quot;大话西游之大圣娶亲&quot;,40.0),<br>(&quot;大话西游之仙履奇缘&quot;,50.0),<br>(&quot;功夫&quot;,60.0)<br></code></pre></td></tr></table></figure>

<h5 id="2-2-逻辑建模"><a href="#2-2-逻辑建模" class="headerlink" title="2.2 逻辑建模"></a>2.2 逻辑建模</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Data</span><br><span class="hljs-meta">@AllArgsConstructor</span><br><span class="hljs-meta">@NoArgsConstructor</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Movie</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> Integer movieId;<br>    <span class="hljs-keyword">private</span> String movieName;<br>    <span class="hljs-keyword">private</span> Double moviePrice;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="3-创建Dao"><a href="#3-创建Dao" class="headerlink" title="3. 创建Dao"></a>3. 创建Dao</h4><h5 id="3-1-接口"><a href="#3-1-接口" class="headerlink" title="3.1 接口"></a>3.1 接口</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.dao;<br><br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.dao</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:01</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MovieDao</span> </span>&#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 查询所有</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function">List&lt;Movie&gt; <span class="hljs-title">getAll</span><span class="hljs-params">()</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据id查询</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movieId</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function">Movie <span class="hljs-title">getMovieById</span><span class="hljs-params">(String movieId)</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 添加Movie</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movie</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">saveMovie</span><span class="hljs-params">(Movie movie)</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 修改Movie</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movie</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据id删除</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movieId</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">removeMovieById</span><span class="hljs-params">(String movieId)</span></span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-2-实现类"><a href="#3-2-实现类" class="headerlink" title="3.2 实现类"></a>3.2 实现类</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.dao.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.dao.MovieDao;<br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.dao.impl</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:03</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MovieDao</span> </span>&#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;Movie&gt; <span class="hljs-title">getAll</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">getMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br><br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h4 id="4-创建Service"><a href="#4-创建Service" class="headerlink" title="4 创建Service"></a>4 创建Service</h4><h5 id="4-1-接口"><a href="#4-1-接口" class="headerlink" title="4.1 接口"></a>4.1 接口</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.service;<br><br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.service</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:03</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">MovieService</span> </span>&#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 查询所有</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function">List&lt;Movie&gt; <span class="hljs-title">getAll</span><span class="hljs-params">()</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据id查询</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movieId</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span></span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function">Movie <span class="hljs-title">getMovieById</span><span class="hljs-params">(String movieId)</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 添加Movie</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movie</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">saveMovie</span><span class="hljs-params">(Movie movie)</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 修改Movie</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movie</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span></span>;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 根据id删除</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> movieId</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">removeMovieById</span><span class="hljs-params">(String movieId)</span></span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="4-2-实现类"><a href="#4-2-实现类" class="headerlink" title="4.2 实现类"></a>4.2 实现类</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.dao.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.dao.MovieDao;<br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MovieDao</span> </span>&#123;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;Movie&gt; <span class="hljs-title">getAll</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">getMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第二节-环境搭建"><a href="#第二节-环境搭建" class="headerlink" title="第二节 环境搭建"></a>第二节 环境搭建</h3><h4 id="1-配置文件"><a href="#1-配置文件" class="headerlink" title="1 配置文件"></a>1 配置文件</h4><h5 id="1-1-web-xml"><a href="#1-1-web-xml" class="headerlink" title="1.1 web.xml"></a>1.1 web.xml</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">web-app</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">version</span>=<span class="hljs-string">&quot;2.5&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--DispatcherServlet--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>dispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-class</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>contextConfigLocation<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>classpath:springmvc.xml<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">load-on-startup</span>&gt;</span>1<span class="hljs-tag">&lt;/<span class="hljs-name">load-on-startup</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">servlet</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">servlet-mapping</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">servlet-name</span>&gt;</span>dispatcherServlet<span class="hljs-tag">&lt;/<span class="hljs-name">servlet-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">servlet-mapping</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--CharacterEncodingFilter--&gt;</span><br>    <span class="hljs-comment">&lt;!-- 配置过滤器解决 POST 请求的字符乱码问题 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">filter</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">filter-class</span>&gt;</span>org.springframework.web.filter.CharacterEncodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-class</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- encoding参数指定要使用的字符集名称 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>encoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- 请求强制编码 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>forceRequestEncoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- 响应强制编码 --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">init-param</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-name</span>&gt;</span>forceResponseEncoding<span class="hljs-tag">&lt;/<span class="hljs-name">param-name</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">param-value</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">param-value</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">init-param</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">filter-mapping</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/*<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">filter-mapping</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">web-app</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-2-logback-xml"><a href="#1-2-logback-xml" class="headerlink" title="1.2 logback.xml"></a>1.2 logback.xml</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">configuration</span> <span class="hljs-attr">debug</span>=<span class="hljs-string">&quot;true&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!-- 指定日志输出的位置 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">appender</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;STDOUT&quot;</span></span><br><span class="hljs-tag">              <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;ch.qos.logback.core.ConsoleAppender&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">encoder</span>&gt;</span><br>            <span class="hljs-comment">&lt;!-- 日志输出的格式 --&gt;</span><br>            <span class="hljs-comment">&lt;!-- 按照顺序分别是：时间、日志级别、线程名称、打印日志的类、日志主体内容、换行 --&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">pattern</span>&gt;</span>[%d&#123;HH:mm:ss.SSS&#125;] [%-5level] [%thread] [%logger] [%msg]%n<span class="hljs-tag">&lt;/<span class="hljs-name">pattern</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">encoder</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">appender</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- 设置全局日志级别。日志级别按顺序分别是：DEBUG、INFO、WARN、ERROR --&gt;</span><br>    <span class="hljs-comment">&lt;!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">root</span> <span class="hljs-attr">level</span>=<span class="hljs-string">&quot;INFO&quot;</span>&gt;</span><br>        <span class="hljs-comment">&lt;!-- 指定打印日志的appender，这里通过“STDOUT”引用了前面配置的appender --&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">appender-ref</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;STDOUT&quot;</span> /&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">root</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- 根据特殊需求指定局部日志级别 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">logger</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;org.springframework.web.servlet.DispatcherServlet&quot;</span> <span class="hljs-attr">level</span>=<span class="hljs-string">&quot;DEBUG&quot;</span> /&gt;</span><br>    <br><span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="1-3-SpringMVC-配置文件springmvc-xml"><a href="#1-3-SpringMVC-配置文件springmvc-xml" class="headerlink" title="1.3 SpringMVC 配置文件springmvc.xml"></a>1.3 SpringMVC 配置文件springmvc.xml</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:context</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/context&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xmlns:mvc</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/mvc&quot;</span></span><br><span class="hljs-tag">       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://www.springframework.org/schema/beans</span></span><br><span class="hljs-string"><span class="hljs-tag">                           http://www.springframework.org/schema/beans/spring-beans.xsd</span></span><br><span class="hljs-string"><span class="hljs-tag">                           http://www.springframework.org/schema/context</span></span><br><span class="hljs-string"><span class="hljs-tag">                           https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd&quot;</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--1.包扫描--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.atguigu&quot;</span>/&gt;</span><br>    <span class="hljs-comment">&lt;!--2. Thymeleaf解析器的配置--&gt;</span><br>    <span class="hljs-comment">&lt;!-- Thymeleaf视图解析器 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;viewResolver&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.thymeleaf.spring5.view.ThymeleafViewResolver&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;order&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;1&quot;</span>/&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;characterEncoding&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>/&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;templateEngine&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.thymeleaf.spring5.SpringTemplateEngine&quot;</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;templateResolver&quot;</span>&gt;</span><br>                    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver&quot;</span>&gt;</span><br><br>                        <span class="hljs-comment">&lt;!-- 视图前缀 --&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;prefix&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;/WEB-INF/templates/&quot;</span>/&gt;</span><br><br>                        <span class="hljs-comment">&lt;!-- 视图后缀 --&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;suffix&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;.html&quot;</span>/&gt;</span><br><br>                        <span class="hljs-comment">&lt;!--模板类型--&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;templateMode&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;HTML5&quot;</span>/&gt;</span><br>                        <span class="hljs-comment">&lt;!--模板的字符编码--&gt;</span><br>                        <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;characterEncoding&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;UTF-8&quot;</span> /&gt;</span><br>                    <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>                <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">property</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br>    <span class="hljs-comment">&lt;!--3. 加载MVC注解驱动--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">mvc:annotation-driven</span> /&gt;</span><br>    <span class="hljs-comment">&lt;!--4. 静态资源处理--&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">mvc:default-servlet-handler</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="第三节-持久层代码"><a href="#第三节-持久层代码" class="headerlink" title="第三节 持久层代码"></a>第三节 持久层代码</h3><h4 id="1-数据源配置文件-jdbc-properties"><a href="#1-数据源配置文件-jdbc-properties" class="headerlink" title="1. 数据源配置文件:jdbc.properties"></a>1. 数据源配置文件:jdbc.properties</h4><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-meta">jdbc.username</span>=<span class="hljs-string">root</span><br><span class="hljs-meta">jdbc.password</span>=<span class="hljs-string">123456</span><br><span class="hljs-meta">jdbc.driver</span>=<span class="hljs-string">com.mysql.jdbc.Driver</span><br><span class="hljs-meta">jdbc.url</span>=<span class="hljs-string">jdbc:mysql://localhost:3306/springmvc-test</span><br></code></pre></td></tr></table></figure>

<h4 id="2-配置数据源-在springmvc-xml中"><a href="#2-配置数据源-在springmvc-xml中" class="headerlink" title="2. 配置数据源:在springmvc.xml中"></a>2. 配置数据源:在springmvc.xml中</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">context:property-placeholder</span> <span class="hljs-attr">location</span>=<span class="hljs-string">&quot;classpath:jdbc.properties&quot;</span>/&gt;</span><br><span class="hljs-comment">&lt;!--5. 对数据源进行IOC--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;dataSource&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;com.alibaba.druid.pool.DruidDataSource&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;username&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.username&#125;&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;password&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.password&#125;&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;url&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.url&#125;&quot;</span>/&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;driverClassName&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;$&#123;jdbc.driver&#125;&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="3-配置JdbcTemplate-在springmvc-xml中"><a href="#3-配置JdbcTemplate-在springmvc-xml中" class="headerlink" title="3. 配置JdbcTemplate:在springmvc.xml中"></a>3. 配置JdbcTemplate:在springmvc.xml中</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--6. 对JdbcTemplate进行IOC--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;jdbcTemplate&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;org.springframework.jdbc.core.JdbcTemplate&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;dataSource&quot;</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;dataSource&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">bean</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="4-配置持久层的实现类"><a href="#4-配置持久层的实现类" class="headerlink" title="4. 配置持久层的实现类"></a>4. 配置持久层的实现类</h4><p>对持久层的实现类进行IOC配置，以及注入JdbcTemplate对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.dao.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.dao.MovieDao;<br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.jdbc.core.JdbcTemplate;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Repository;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.dao.impl</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:03</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Repository</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MovieDao</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> JdbcTemplate jdbcTemplate;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;Movie&gt; <span class="hljs-title">getAll</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">getMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br><br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="5-编写测试持久层的各个方法"><a href="#5-编写测试持久层的各个方法" class="headerlink" title="5. 编写测试持久层的各个方法"></a>5. 编写测试持久层的各个方法</h4><h5 id="5-1-持久层的各个方法实现"><a href="#5-1-持久层的各个方法实现" class="headerlink" title="5.1 持久层的各个方法实现"></a>5.1 持久层的各个方法实现</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.dao.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.dao.MovieDao;<br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.jdbc.core.BeanPropertyRowMapper;<br><span class="hljs-keyword">import</span> org.springframework.jdbc.core.JdbcTemplate;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Repository;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.dao.impl</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:03</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Repository</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MovieDao</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> JdbcTemplate jdbcTemplate;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;Movie&gt; <span class="hljs-title">getAll</span><span class="hljs-params">()</span> </span>&#123;<br>        String sql = <span class="hljs-string">&quot;select movie_id movieId, movie_name movieName,movie_price moviePrice from t_movie&quot;</span>;<br>        <span class="hljs-keyword">return</span> jdbcTemplate.query(sql,<span class="hljs-keyword">new</span> BeanPropertyRowMapper&lt;&gt;(Movie.class));<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">getMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br>        String sql = <span class="hljs-string">&quot;select movie_id movieId, movie_name movieName,movie_price moviePrice from t_movie where movie_id=?&quot;</span>;<br>        <span class="hljs-keyword">return</span> jdbcTemplate.queryForObject(sql,<span class="hljs-keyword">new</span> BeanPropertyRowMapper&lt;&gt;(Movie.class),movieId);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br>        String sql = <span class="hljs-string">&quot;insert into t_movie(movie_name,movie_price) values (?,?)&quot;</span>;<br>        jdbcTemplate.update(sql,movie.getMovieName(),movie.getMoviePrice());<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br>        String sql = <span class="hljs-string">&quot;update t_movie set movie_name=?,movie_price=? where movie_id=?&quot;</span>;<br>        jdbcTemplate.update(sql,movie.getMovieName(),movie.getMoviePrice(),movie.getMovieId());<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br>        String sql = <span class="hljs-string">&quot;delete from t_movie where movie_id=?&quot;</span>;<br>        jdbcTemplate.update(sql,movieId);<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h5 id="5-2-持久层方法测试"><a href="#5-2-持久层方法测试" class="headerlink" title="5.2 持久层方法测试"></a>5.2 持久层方法测试</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu;<br><br><span class="hljs-keyword">import</span> com.atguigu.dao.MovieDao;<br><span class="hljs-keyword">import</span> org.junit.Test;<br><span class="hljs-keyword">import</span> org.junit.runner.RunWith;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.test.context.ContextConfiguration;<br><span class="hljs-keyword">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:22</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@RunWith(SpringJUnit4ClassRunner.class)</span><br><span class="hljs-meta">@ContextConfiguration(locations = &quot;classpath:springmvc.xml&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestDao</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> MovieDao movieDao;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testGetAll</span><span class="hljs-params">()</span></span>&#123;<br>        System.out.println(movieDao.getAll());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="第四节-业务层的代码"><a href="#第四节-业务层的代码" class="headerlink" title="第四节 业务层的代码"></a>第四节 业务层的代码</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.service.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.dao.MovieDao;<br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><span class="hljs-keyword">import</span> com.atguigu.service.MovieService;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Service;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.service.impl</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:04</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Service</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">MovieService</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> MovieDao movieDao;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;Movie&gt; <span class="hljs-title">getAll</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> movieDao.getAll();<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> Movie <span class="hljs-title">getMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> movieDao.getMovieById(movieId);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">saveMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br>        movieDao.saveMovie(movie);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br>        movieDao.updateMovie(movie);<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeMovieById</span><span class="hljs-params">(String movieId)</span> </span>&#123;<br>        movieDao.removeMovieById(movieId);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第五节-实现功能"><a href="#第五节-实现功能" class="headerlink" title="第五节 实现功能"></a>第五节 实现功能</h3><h4 id="1-显示首页"><a href="#1-显示首页" class="headerlink" title="1. 显示首页"></a>1. 显示首页</h4><h5 id="1-1-案例流程"><a href="#1-1-案例流程" class="headerlink" title="1.1 案例流程"></a>1.1 案例流程</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_018.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="1-2-具体实现"><a href="#1-2-具体实现" class="headerlink" title="1.2 具体实现"></a>1.2 具体实现</h5><h6 id="1-2-1-配置-view-controller"><a href="#1-2-1-配置-view-controller" class="headerlink" title="1.2.1 配置 view-controller"></a>1.2.1 配置 view-controller</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--7. 使用view-controller访问首页--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mvc:view-controller</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/&quot;</span> <span class="hljs-attr">view-name</span>=<span class="hljs-string">&quot;portal&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mvc:view-controller</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/index.html&quot;</span> <span class="hljs-attr">view-name</span>=<span class="hljs-string">&quot;portal&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="1-2-2-前端页面"><a href="#1-2-2-前端页面" class="headerlink" title="1.2.2 前端页面"></a>1.2.2 前端页面</h6><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_019.png" srcset="/img/loading.gif" lazyload></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span> <span class="hljs-attr">xmlns:th</span>=<span class="hljs-string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;text-align: center&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/movie/showList&#125;&quot;</span>&gt;</span>显示电影列表<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="2-在首页点击超链接显示全部数据"><a href="#2-在首页点击超链接显示全部数据" class="headerlink" title="2. 在首页点击超链接显示全部数据"></a>2. 在首页点击超链接显示全部数据</h4><h5 id="2-1-案例流程"><a href="#2-1-案例流程" class="headerlink" title="2.1 案例流程"></a>2.1 案例流程</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_020.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="2-2-具体实现"><a href="#2-2-具体实现" class="headerlink" title="2.2 具体实现"></a>2.2 具体实现</h5><h6 id="2-2-1-处理器类和处理器方法"><a href="#2-2-1-处理器类和处理器方法" class="headerlink" title="2.2.1 处理器类和处理器方法"></a>2.2.1 处理器类和处理器方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.handler;<br><br><span class="hljs-keyword">import</span> com.atguigu.pojo.Movie;<br><span class="hljs-keyword">import</span> com.atguigu.service.MovieService;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Controller;<br><span class="hljs-keyword">import</span> org.springframework.ui.Model;<br><span class="hljs-keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;<br><br><span class="hljs-keyword">import</span> java.util.List;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.handler</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> Leevi</span><br><span class="hljs-comment"> * 日期2021-09-06  10:31</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Controller</span><br><span class="hljs-meta">@RequestMapping(&quot;/movie&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MovieHandler</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> MovieService movieService;<br><br>    <span class="hljs-meta">@RequestMapping(&quot;/showList&quot;)</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">showList</span><span class="hljs-params">(Model model)</span></span>&#123;<br>        <span class="hljs-comment">//1. 调用业务层的方法查询所有的movie</span><br>        List&lt;Movie&gt; movieList = movieService.getAll();<br>        <span class="hljs-comment">//2. 将查询到的所有movie存储到请求域</span><br>        model.addAttribute(<span class="hljs-string">&quot;movieList&quot;</span>,movieList);<br>        <span class="hljs-comment">//3. 解析Thymeleaf模板显示所有movie</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;list&quot;</span>;<br>    &#125;<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h6 id="2-2-2-前端页面"><a href="#2-2-2-前端页面" class="headerlink" title="2.2.2 前端页面"></a>2.2.2 前端页面</h6><ol>
<li>样式部分</li>
</ol>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span><br><span class="css">    <span class="hljs-selector-tag">table</span> &#123;</span><br><span class="css">        <span class="hljs-attribute">border-collapse</span>: collapse;</span><br><span class="css">        <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span> auto <span class="hljs-number">0px</span> auto;</span><br><span class="css">    &#125;</span><br><span class="css">    <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">th</span>,<span class="hljs-selector-tag">td</span> &#123;</span><br><span class="css">        <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid black;</span><br><span class="css">        <span class="hljs-attribute">text-align</span>: center;</span><br><span class="css">    &#125;</span><br><span class="css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>

<ol start="2">
<li>数据展示部分</li>
</ol>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>电影ID<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>电影名称<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>电影票价格<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>更新<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span> <span class="hljs-attr">th:if</span>=<span class="hljs-string">&quot;$&#123;#lists.isEmpty(movieList)&#125;&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">colspan</span>=<span class="hljs-string">&quot;5&quot;</span>&gt;</span>抱歉！没有查询到数据！<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span> <span class="hljs-attr">th:unless</span>=<span class="hljs-string">&quot;$&#123;#lists.isEmpty(movieList)&#125;&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span> <span class="hljs-attr">th:each</span>=<span class="hljs-string">&quot;movie : $&#123;movieList&#125;&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">th:text</span>=<span class="hljs-string">&quot;$&#123;movie.movieId&#125;&quot;</span>&gt;</span>电影ID<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">th:text</span>=<span class="hljs-string">&quot;$&#123;movie.movieName&#125;&quot;</span>&gt;</span>电影名称<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">th:text</span>=<span class="hljs-string">&quot;$&#123;movie.moviePrice&#125;&quot;</span>&gt;</span>电影票价格<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>更新<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">tfoot</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">colspan</span>=<span class="hljs-string">&quot;5&quot;</span>&gt;</span>添加<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">tfoot</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="3-删除一条数据"><a href="#3-删除一条数据" class="headerlink" title="3. 删除一条数据"></a>3. 删除一条数据</h4><h5 id="3-1-案例流程"><a href="#3-1-案例流程" class="headerlink" title="3.1 案例流程"></a>3.1 案例流程</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_021.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="3-2-具体实现"><a href="#3-2-具体实现" class="headerlink" title="3.2 具体实现"></a>3.2 具体实现</h5><h6 id="3-2-1-修改数据展示时候的删除数据的超链接"><a href="#3-2-1-修改数据展示时候的删除数据的超链接" class="headerlink" title="3.2.1 修改数据展示时候的删除数据的超链接"></a>3.2.1 修改数据展示时候的删除数据的超链接</h6><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/movie/removeMovie(movieId=$&#123;movie.movieId&#125;)&#125;&quot;</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="3-2-2-handler-方法"><a href="#3-2-2-handler-方法" class="headerlink" title="3.2.2 handler 方法"></a>3.2.2 handler 方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/removeMovie&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">removeMovie</span><span class="hljs-params">(<span class="hljs-meta">@RequestParam(&quot;movieId&quot;)</span> String movieId)</span></span>&#123;<br>    <span class="hljs-comment">//调用业务层的方法根据id删除movie</span><br>    movieService.removeMovieById(movieId);<br>    <span class="hljs-comment">//重新查询所有</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;redirect:/movie/showList&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="4-跳转到添加页面"><a href="#4-跳转到添加页面" class="headerlink" title="4. 跳转到添加页面"></a>4. 跳转到添加页面</h4><h5 id="4-1-案例流程"><a href="#4-1-案例流程" class="headerlink" title="4.1  案例流程"></a>4.1  案例流程</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_022.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="4-2-具体实现"><a href="#4-2-具体实现" class="headerlink" title="4.2 具体实现"></a>4.2 具体实现</h5><h6 id="4-2-1-修改页面上添加数据的超链接"><a href="#4-2-1-修改页面上添加数据的超链接" class="headerlink" title="4.2.1 修改页面上添加数据的超链接"></a>4.2.1 修改页面上添加数据的超链接</h6><p>在<code>tfoot</code>标签的td中加入超链接标签 </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">colspan</span>=<span class="hljs-string">&quot;5&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/add.html&#125;&quot;</span>&gt;</span>添加<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="4-2-2-配置view-controller"><a href="#4-2-2-配置view-controller" class="headerlink" title="4.2.2 配置view-controller"></a>4.2.2 配置view-controller</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--8. 使用view-controller访问添加页面--&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">mvc:view-controller</span> <span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/add.html&quot;</span> <span class="hljs-attr">view-name</span>=<span class="hljs-string">&quot;add&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="4-2-3-movie-add-html页面的表单"><a href="#4-2-3-movie-add-html页面的表单" class="headerlink" title="4.2.3 movie-add.html页面的表单"></a>4.2.3 movie-add.html页面的表单</h6><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">th:action</span>=<span class="hljs-string">&quot;@&#123;/movie/saveMovie&#125;&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br><br>    电影名称：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;movieName&quot;</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    电影票价格：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;moviePrice&quot;</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span>&gt;</span>保存<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br><br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="5-执行添加数据"><a href="#5-执行添加数据" class="headerlink" title="5. 执行添加数据"></a>5. 执行添加数据</h4><h5 id="5-1-案例流程"><a href="#5-1-案例流程" class="headerlink" title="5.1 案例流程"></a>5.1 案例流程</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_023.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="5-2-具体实现"><a href="#5-2-具体实现" class="headerlink" title="5.2 具体实现"></a>5.2 具体实现</h5><h6 id="5-2-1-handler方法"><a href="#5-2-1-handler方法" class="headerlink" title="5.2.1 handler方法"></a>5.2.1 handler方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/saveMovie&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">addMovie</span><span class="hljs-params">(Movie movie)</span></span>&#123;<br>    <span class="hljs-comment">//调用业务层的方法添加Movie</span><br>    movieService.saveMovie(movie);<br>    <span class="hljs-comment">//重新查询所有</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;redirect:/movie/showList&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="6-跳转到更新表单回显数据"><a href="#6-跳转到更新表单回显数据" class="headerlink" title="6. 跳转到更新表单回显数据"></a>6. 跳转到更新表单回显数据</h4><h5 id="6-1-案例流程"><a href="#6-1-案例流程" class="headerlink" title="6.1 案例流程"></a>6.1 案例流程</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_021.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="6-2-具体实现"><a href="#6-2-具体实现" class="headerlink" title="6.2 具体实现"></a>6.2 具体实现</h5><h6 id="6-2-1-修改页面更新数据的超链接"><a href="#6-2-1-修改页面更新数据的超链接" class="headerlink" title="6.2.1 修改页面更新数据的超链接"></a>6.2.1 修改页面更新数据的超链接</h6><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">th:href</span>=<span class="hljs-string">&quot;@&#123;/edit/movie/page(movieId=$&#123;movie.movieId&#125;)&#125;&quot;</span>&gt;</span>更新<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h6 id="6-2-2-handler方法"><a href="#6-2-2-handler方法" class="headerlink" title="6.2.2 handler方法"></a>6.2.2 handler方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/edit/movie/page&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">editMoviePage</span><span class="hljs-params">(</span></span><br><span class="hljs-params"><span class="hljs-function">    </span></span><br><span class="hljs-params"><span class="hljs-function">        // 获取请求参数</span></span><br><span class="hljs-params"><span class="hljs-function">        <span class="hljs-meta">@RequestParam(&quot;movieId&quot;)</span> String movieId,</span></span><br><span class="hljs-params"><span class="hljs-function">        </span></span><br><span class="hljs-params"><span class="hljs-function">        Model model</span></span><br><span class="hljs-params"><span class="hljs-function">)</span> </span>&#123;<br>    <br>    <span class="hljs-comment">// 1.根据id查询movie对象</span><br>    Movie movie = movieService.getMovieById(movieId);<br>        <br>    <span class="hljs-comment">// 2.将movie对象存入模型</span><br>    model.addAttribute(<span class="hljs-string">&quot;movie&quot;</span>, movie);<br>    <br>    <span class="hljs-comment">// 3.返回逻辑视图</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;movie-edit&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="6-2-3-准备表单页面并回显"><a href="#6-2-3-准备表单页面并回显" class="headerlink" title="6.2.3 准备表单页面并回显"></a>6.2.3 准备表单页面并回显</h6><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">th:action</span>=<span class="hljs-string">&quot;@&#123;/update/movie&#125;&quot;</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span><br>    <br>    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;hidden&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;movieId&quot;</span> <span class="hljs-attr">th:value</span>=<span class="hljs-string">&quot;$&#123;movie.movieId&#125;&quot;</span> /&gt;</span><br>    <br>    电影名称：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;movieName&quot;</span> <span class="hljs-attr">th:value</span>=<span class="hljs-string">&quot;$&#123;movie.movieName&#125;&quot;</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    电影票价格：<span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;moviePrice&quot;</span> <span class="hljs-attr">th:value</span>=<span class="hljs-string">&quot;$&#123;movie.moviePrice&#125;&quot;</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span><br>    <br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span>&gt;</span>更新<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <br><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="7-执行更新"><a href="#7-执行更新" class="headerlink" title="7. 执行更新"></a>7. 执行更新</h4><h5 id="7-1-案例流程"><a href="#7-1-案例流程" class="headerlink" title="7.1 案例流程"></a>7.1 案例流程</h5><p><img src="http://www.slx.blue/2021/12/10/springMVC-01/tu_025.png" srcset="/img/loading.gif" lazyload></p>
<h5 id="7-2-具体实现"><a href="#7-2-具体实现" class="headerlink" title="7.2 具体实现"></a>7.2 具体实现</h5><h6 id="7-2-1-handler方法"><a href="#7-2-1-handler方法" class="headerlink" title="7.2.1 handler方法"></a>7.2.1 handler方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RequestMapping(&quot;/update/movie&quot;)</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">updateMovie</span><span class="hljs-params">(Movie movie)</span> </span>&#123;<br>    <br>    movieService.updateMovie(movie);<br>    <br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;redirect:/show/list&quot;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第六节-案例环境总结"><a href="#第六节-案例环境总结" class="headerlink" title="第六节 案例环境总结"></a>第六节 案例环境总结</h3><h4 id="1-Spring和SpringMVC的环境"><a href="#1-Spring和SpringMVC的环境" class="headerlink" title="1. Spring和SpringMVC的环境"></a>1. Spring和SpringMVC的环境</h4><ol>
<li>依赖</li>
<li>web.xml中的配置:servlet、filter</li>
<li>springmvc配置文件:<ol>
<li>包扫描:为了解析组件注解(IOC和依赖注入的主键)</li>
<li>配置Thymeleaf的模板解析器:为了解析Thymeleaf模板</li>
<li>加载MVC注解驱动:为了能够找到Handler方法处理请求</li>
<li>处理静态资源</li>
<li><code>view-controller</code>:访问页面</li>
</ol>
</li>
<li>注解: <ol>
<li>IOC和依赖注入的注解: Controller、Service、Repository、AutoWired</li>
<li>SpringMVC的注解: RequestMapping(请求映射)、RequestParam(获取请求参数)</li>
</ol>
</li>
<li>日志</li>
</ol>
<h4 id="2-持久层环境"><a href="#2-持久层环境" class="headerlink" title="2. 持久层环境"></a>2. 持久层环境</h4><ol>
<li>依赖</li>
<li>springmvc配置文件中配置持久层:<ol>
<li>数据源</li>
<li>JdbcTemplate</li>
</ol>
</li>
<li>持久层实现类的方法中执行各个SQL语句</li>
</ol>
<h4 id="3-测试环境"><a href="#3-测试环境" class="headerlink" title="3. 测试环境"></a>3. 测试环境</h4><ol>
<li>依赖</li>
<li>Spring整合Junit</li>
<li>测试:需要测试业务层和持久层的代码</li>
</ol>
<h3 id="第七节-功能总结"><a href="#第七节-功能总结" class="headerlink" title="第七节 功能总结"></a>第七节 功能总结</h3><h4 id="1-单纯跳转页面的功能"><a href="#1-单纯跳转页面的功能" class="headerlink" title="1. 单纯跳转页面的功能"></a>1. 单纯跳转页面的功能</h4><p>例如:访问首页、访问add.html添加页面，使用<code>view-controller</code>标签实现</p>
<h4 id="2-查询功能"><a href="#2-查询功能" class="headerlink" title="2. 查询功能"></a>2. 查询功能</h4><p>例如:查询电影列表、更新前的数据回显。它们的具体步骤:</p>
<ol>
<li>需不需要带参数:就要看有没有查询条件</li>
<li>获取参数:</li>
<li>调用业务层的方法进行查询，获取到查询的数据</li>
<li>将查询到的数据存储到请求域</li>
<li>返回逻辑视图</li>
<li>在Thymeleaf页面获取请求域的数据，并展示</li>
</ol>
<h4 id="3-增删改功能"><a href="#3-增删改功能" class="headerlink" title="3. 增删改功能"></a>3. 增删改功能</h4><p>例如:添加电影、删除电影、更新电影。它们的具体步骤:</p>
<ol>
<li>肯定需要带参数:Thymeleaf的路径携带参数、以及表单携带参数</li>
<li>参数乱码:配置过滤器解决</li>
<li>获取参数:单个参数、POJO封装参数</li>
<li>调用业务层的方法进行增删改</li>
<li>重新查询所有: 使用<code>redirect</code>指令重定向访问<code>查询所有电影</code>的功能</li>
</ol>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/springMVC/">springMVC</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/springMVC/">springMVC</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客目前大部分文章都是参考尚硅谷或者马士兵教育的学习资料！<a href="http://www.atguigu.com/" rel="nofollow noopener"官网地址！</a> 
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/11/springMVC-02/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">springmvc-day02</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/12/09/java-mediator/">
                        <span class="hidden-mobile">java-中介者模式</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">

  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('11/02/2021 21:39:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
